package managedbean;

import entity.AddressEntity;
import entity.AnalyticsEntity;
import entity.FnBBranchEntity;
import entity.PaymentManagementEntity;
import entity.ServiceEntity;
import entity.ServicePackageEntity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;
import session.stateful.StakeholderSessionBeanStateful;
import javax.faces.context.FacesContext;
import java.util.List;
import entity.StakeholderEntity;
import entity.TCubeEntity;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import org.primefaces.event.SelectEvent;
import session.stateful.MetricSessionBean;
import session.stateful.ServiceSessionBean;
import session.stateful.TCubeSessionBean;

@ManagedBean(name = "tCubeManagerBean")
@SessionScoped
public class TCubeManagerBean implements Serializable {

    @EJB
    private StakeholderSessionBeanStateful stakeholderSessionBean;
    @EJB
    private ServiceSessionBean serviceSessionBean;
    @EJB
    private MetricSessionBean metricSessionBean;
    @EJB
    private TCubeSessionBean tCubeSessionBean;
    /*
     * Common attributes
     */
    private String statusMessage;
    private AddressEntity stakeholderAddress;
    private AddressEntity branchAddress;
    private StakeholderEntity selectedStakeholder;
    private FnBBranchEntity selectedBranch;
    private TCubeEntity tCube;
    private long tCubeId;
    //This list is the priority of service package for ranking in advertisement
    private List listOfPriority;
    /*
     * End of common attributes
     */
    /*
     * Attributes related to service
     */
    private ServiceEntity selectedService;
    private ServiceEntity service;
    private boolean addService;
    /*
     * End attributes related to service
     */
    /*
     * Attributes related to service package
     */
    private ServicePackageEntity selectedServicePackage;
    private ServicePackageEntity servicePackage;
    private ServiceEntity[] selectedServices;
    private ServiceEntity[] services;
    private List<ServiceEntity> listOfServices;
    private List<AnalyticsEntity> listOfAnalytics;
    private AnalyticsEntity[] selectedAnalytics;
    private boolean addServicePackage;
    private boolean editServicePackage;
    /*
     * End attributes related to service package
     */

    public TCubeManagerBean() {
        selectedBranch = new FnBBranchEntity();
        servicePackage = new ServicePackageEntity();
        addServicePackage = true;
        addService = true;
        listOfPriority = new ArrayList();
        for (int i = 1; i <= 10; i++) {
            listOfPriority.add(new SelectItem(i, i + ""));
        }
    }

    public void initAddServicePackage(PhaseEvent event) {
        initList();
        //if this page is loaded for adding a new service package then refresh every attribute
        //related to service package to the original value
        if (addServicePackage) {
            servicePackage = new ServicePackageEntity();
            services = null;
            selectedAnalytics = null;
            addServicePackage = false;
        }
    }

    public void initAddService(PhaseEvent event) {
        if (addService) {
            service = new ServiceEntity();
            addService = false;
        }
    }

    public void initEditServicePackage(PhaseEvent event) {
        initList();
        if (editServicePackage) {
            selectedServices = serviceSessionBean.getServiceByServicePackageId(selectedServicePackage.getId());
            selectedAnalytics = metricSessionBean.getAnalyticsByServicePackageId(selectedServicePackage.getId());

            editServicePackage = false;
            addServicePackage = true;
        }
    }

    private void initList() {
        if (listOfServices == null) {
            listOfServices = serviceSessionBean.getServices();
        }

        if (listOfAnalytics == null) {
            listOfAnalytics = metricSessionBean.getAnalyticsForStakeholder();
        }
    }

    public void initViewAllService(PhaseEvent event) {
        listOfServices = serviceSessionBean.getServices();
    }

    public void selectNewServicePackage(SelectEvent event) {
        editServicePackage = true;
    }

    public List<ServicePackageEntity> getServicePackages() {
        return serviceSessionBean.getServicePackages();
    }

    public void retrieveStakeholderInfo() {
        selectedStakeholder = getStakeholderSessionBean().retrieveStakeholderInfoByUsername(selectedStakeholder.getUsername());
    }

    public List<StakeholderEntity> getStakeholders() {
        return stakeholderSessionBean.getAllStakeholders();
    }

    public String onRowSelectNavigate(SelectEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedStakeholder", event.getObject());

        return "staff/TCubeEditStakeholderInfo?faces-redirect=true";
    }

    public String onRowSelectNavigateService(SelectEvent event) {
        return "staff/TCubeEditServiceInfo?faces-redirect=true";
    }

    public String onRowSelectNavigateServicePackage(SelectEvent event) {
        editServicePackage = true;
        return "staff/TCubeEditServicePackageInfo?faces-redirect=true";
    }

    public void editStakeholderInfo() {
        stakeholderSessionBean.editStakeholderInfo(selectedStakeholder);
    }

    public void editServiceInfo() {
        serviceSessionBean.editServiceInfo(selectedService);
    }

    public void addService() {
        serviceSessionBean.createNewService(service);
        listOfServices = serviceSessionBean.getServices();
        service = new ServiceEntity();
    }

    public void addServicePackage() {
        serviceSessionBean.createNewServicePackage(servicePackage, services, selectedAnalytics);
        addServicePackage = true;
    }

    public void editServicePackageInfo(ActionEvent event) {
        serviceSessionBean.editServicePackageInfo(selectedServicePackage, selectedServices, selectedAnalytics);
    }

    public StakeholderSessionBeanStateful getStakeholderSessionBean() {
        return stakeholderSessionBean;
    }

    public void setStakeholderSessionBean(StakeholderSessionBeanStateful stakeholderSessionBean) {
        this.stakeholderSessionBean = stakeholderSessionBean;
    }

    public ServiceSessionBean getServiceSessionBean() {
        return serviceSessionBean;
    }

    public void setServiceSessionBean(ServiceSessionBean serviceSessionBean) {
        this.serviceSessionBean = serviceSessionBean;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public AddressEntity getStakeholderAddress() {
        return stakeholderAddress;
    }

    public void setStakeholderAddress(AddressEntity stakeholderAddress) {
        this.stakeholderAddress = stakeholderAddress;
    }

    public AddressEntity getBranchAddress() {
        return branchAddress;
    }

    public void setBranchAddress(AddressEntity branchAddress) {
        this.branchAddress = branchAddress;
    }

    public StakeholderEntity getSelectedStakeholder() {
        return selectedStakeholder;
    }

    public void setSelectedStakeholder(StakeholderEntity selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
    }

    public FnBBranchEntity getSelectedBranch() {
        return selectedBranch;
    }

    public void setSelectedBranch(FnBBranchEntity selectedBranch) {
        this.selectedBranch = selectedBranch;
    }

    public ServiceEntity getSelectedService() {
        return selectedService;
    }

    public void setSelectedService(ServiceEntity selectedService) {
        this.selectedService = selectedService;
    }

    public ServicePackageEntity getSelectedServicePackage() {
        return selectedServicePackage;
    }

    public void setSelectedServicePackage(ServicePackageEntity selectedServicePackage) {
        this.selectedServicePackage = selectedServicePackage;
    }

    public ServiceEntity[] getSelectedServices() {
        return selectedServices;
    }

    public void setSelectedServices(ServiceEntity[] selectedServices) {
        this.selectedServices = selectedServices;
    }

    public ServicePackageEntity getServicePackage() {
        return servicePackage;
    }

    public void setServicePackage(ServicePackageEntity servicePackage) {
        this.servicePackage = servicePackage;
    }

    public ServiceEntity getService() {
        return service;
    }

    public void setService(ServiceEntity service) {
        this.service = service;
    }

    public ServiceEntity[] getServices() {
        return services;
    }

    public void setServices(ServiceEntity[] services) {
        this.services = services;
    }
    // This method is added by Cong Dat
    private List<PaymentManagementEntity> paymentManagementList;

    public List<PaymentManagementEntity> getPaymentManagementList() {
        return paymentManagementList;
    }

    public void setPaymentManagementList(List<PaymentManagementEntity> paymentManagementList) {
        this.paymentManagementList = paymentManagementList;
    }

    public void initBillListForTCube(PhaseEvent event) {
        this.setPaymentManagementList(stakeholderSessionBean.getBillListOfAllStakeholders());
    }

    public List<ServiceEntity> getListOfServices() {
        return listOfServices;
    }

    public void setListOfServices(List<ServiceEntity> listOfServices) {
        this.listOfServices = listOfServices;
    }

    public List<AnalyticsEntity> getListOfAnalytics() {
        return listOfAnalytics;
    }

    public void setListOfAnalytics(List<AnalyticsEntity> listOfAnalytics) {
        this.setListOfAnalytics(listOfAnalytics);
    }

    public AnalyticsEntity[] getSelectedAnalytics() {
        return selectedAnalytics;
    }

    public void setSelectedAnalytics(AnalyticsEntity[] selectedAnalytics) {
        this.selectedAnalytics = selectedAnalytics;
    }

    public List getListOfPriority() {
        return listOfPriority;
    }

    public void setListOfPriority(List listOfPriority) {
        this.listOfPriority = listOfPriority;
    }
    
    public void retrieveTCubeById(ActionEvent event) {
        tCube = tCubeSessionBean.retrieveTCubeInfoById(tCubeId);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("TCube", tCube);
    }

    public long getTCubeId() {
        return tCubeId;
    }

    public void setTCubeId(long tCubeId) {
        this.tCubeId = tCubeId;
    }
}
